package com.ts.api.http.org.base;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyLaiDaJinFu;
import com.ts.api.http.org.entity.apply.OrgApplyLaiDaJinFuData;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeLaiDaJinFu;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 *  融享客
 *
 *  @author WJB
 */
public class ApiOrgPhoneMd5RongXiangKe implements ApiOrgPhoneMd5ConvertHandler {

    private  final String accessKey;
    private  final String privateKey;
    private  final String iv;
    private  final String code;

    public ApiOrgPhoneMd5RongXiangKe(String accessKey, String privateKey, String iv, String code) {
        this.accessKey = accessKey;
        this.privateKey = privateKey;
        this.iv = iv;
        this.code = code;
    }

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        OrgDataExchangeLaiDaJinFu orgDataExchangeLaiDaJinFu = new OrgDataExchangeLaiDaJinFu();
        orgDataExchangeLaiDaJinFu.setSourceCode(code);
        orgDataExchangeLaiDaJinFu.setAccessKey(accessKey);
        orgDataExchangeLaiDaJinFu.setPhoneMd5(apiDataExchangeParameter.getPhoneMd5());

        return JsonUtils.toJson(orgDataExchangeLaiDaJinFu);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        boolean code = Eq.str(jsonObject.getString("code"), "1");

        JSONObject data = jsonObject.getJSONObject("data");
        if (data == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        Boolean phoneMd5Exist = data.getBoolean("phoneMd5Exist");
        if (phoneMd5Exist == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!(code && !phoneMd5Exist)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public String apply(ApiApplyParameter apiApplyParameter) {
        OrgApplyLaiDaJinFu orgApplyLaiDaJinFu = new OrgApplyLaiDaJinFu();
        orgApplyLaiDaJinFu.setAccessKey(accessKey);
        orgApplyLaiDaJinFu.setSourceCode(code);

        OrgApplyLaiDaJinFuData orgApplyLaiDaJinFuData = new OrgApplyLaiDaJinFuData();
        orgApplyLaiDaJinFuData.setRealName(apiApplyParameter.getUserName());
        orgApplyLaiDaJinFuData.setPhone(apiApplyParameter.getPhone());
        orgApplyLaiDaJinFuData.setCity(apiApplyParameter.getCity());

        //  年龄
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        orgApplyLaiDaJinFuData.setAge(apiFieldConverter.getAgeInt());

        //  贷款金额
        String loanAmount = apiApplyParameter.getLoanAmount();
        Integer loanAmountInt = ChooseEq.create()
                .when("1", loanAmount, 3)
                .when("2", loanAmount, 10)
                .when("3", loanAmount, 20)
                .when("4", loanAmount, 20)
                .end(1);
        orgApplyLaiDaJinFuData.setLoanMoney(loanAmountInt);

        //  职业
        String career = apiApplyParameter.getCareer();
        Integer choose = ChooseEq.create()
                .when(career, "1", 1)
                .when(career, "2", 4)
                .when(career, "3", 2)
                .when(career, "4", 5)
                .when(career, "5", 3)
                //  自由职业
                .end(6);
        orgApplyLaiDaJinFuData.setProfession(choose);

        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 1)
                .when(apiApplyParameter.getSesameCredit(), "2", 2)
                .when(apiApplyParameter.getSesameCredit(), "3", 3)
                .when(apiApplyParameter.getSesameCredit(), "4", 4)
                .end(0);
        orgApplyLaiDaJinFuData.setSesameScore(zhima);

        //  公积金
        Integer gongjijin = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 0)
                .when(apiApplyParameter.getProvidentFund(), "2", 1)
                .when(apiApplyParameter.getProvidentFund(), "3", 2)
                .end(1);
        orgApplyLaiDaJinFuData.setGjjScop(gongjijin);

        //  社保
        Integer sbScop = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 0)
                .when(apiApplyParameter.getSocialSecurity(), "2", 1)
                .when(apiApplyParameter.getSocialSecurity(), "3", 2)
                .end(1);
        orgApplyLaiDaJinFuData.setSbScop(sbScop);

        //  月收入
        Integer incomeMonth = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", 3000)
                .when(apiApplyParameter.getMonthlySalary(), "2", 8000)
                .when(apiApplyParameter.getMonthlySalary(), "3", 15000)
                .when(apiApplyParameter.getMonthlySalary(), "4", 30000)
                .when(apiApplyParameter.getMonthlySalary(), "5", 50000)
                .end();
        orgApplyLaiDaJinFuData.setIncomeMonth(incomeMonth);

        //借款用途
        Integer loanUse = ChooseEq.create()
                .when(apiApplyParameter.getLoanPurpose(), "1", 6)
                .when(apiApplyParameter.getLoanPurpose(), "2", 8)
                .when(apiApplyParameter.getLoanPurpose(), "3", 5)
                .when(apiApplyParameter.getLoanPurpose(), "4", 3)
                .when(apiApplyParameter.getLoanPurpose(), "5", 7)
                .when(apiApplyParameter.getLoanPurpose(), "6", 9)
                .end(9);
        orgApplyLaiDaJinFuData.setLoanUse(loanUse);

        //工资发放形式
        Integer payoffType = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 3)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 2)
                .end(3);
        orgApplyLaiDaJinFuData.setPayoffType(payoffType);

        //  房产情况
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 2)
                .end(1);
        orgApplyLaiDaJinFuData.setHouse(house);

        //  对公账户经营收入
        Integer turnoverPublic = ChooseEq.create()
                .when(apiApplyParameter.getBizBankDetails(), "1", 50)
                .when(apiApplyParameter.getBizBankDetails(), "2", 100)
                .when(apiApplyParameter.getBizBankDetails(), "3", 500)
                .end(0);
        orgApplyLaiDaJinFuData.setTurnoverPublic(turnoverPublic);

        //  车产
        Integer car = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 0)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 2)
                .end(1);
        orgApplyLaiDaJinFuData.setCar(car);

        //  是否办理执照
        Integer hasBusinessLicense = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 0)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 2)
                .when(apiApplyParameter.getCarInfo(), "4", 3)
                .end(0);
        orgApplyLaiDaJinFuData.setHasBusinessLicense(hasBusinessLicense);

        //  学历
        if (ValidParam.isNotEmpty(apiApplyParameter.getEducationAttainment())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getEducationAttainment(), "1", 1)
                    .when(apiApplyParameter.getEducationAttainment(), "2", 1)
                    .when(apiApplyParameter.getEducationAttainment(), "3", 1)
                    .when(apiApplyParameter.getEducationAttainment(), "4", 2)
                    .when(apiApplyParameter.getEducationAttainment(), "5", 3)
                    .when(apiApplyParameter.getEducationAttainment(), "6", 5)
                    .end();
            orgApplyLaiDaJinFuData.setEducation(text);
        } else {
            orgApplyLaiDaJinFuData.setEducation(4);
        }
        //  当前单位工龄
        Integer workingYears = ChooseEq.create()
                .when(apiApplyParameter.getYearsService(), "1", 1)
                .when(apiApplyParameter.getYearsService(), "2", 2)
                .when(apiApplyParameter.getYearsService(), "3", 3)
                .end(1);
        orgApplyLaiDaJinFuData.setWorkingYears(workingYears);

        //  信用卡
        Integer creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 0)
                .end(1);
        orgApplyLaiDaJinFuData.setCreditCard(creditCard);

        //  性别
        String sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "1", "0")
                .end("1");
        orgApplyLaiDaJinFuData.setSex(sex);

        //保单
        Integer baodanScop = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 2)
                .end(1);
        orgApplyLaiDaJinFuData.setBaodanScop(baodanScop);

        //微粒贷
        orgApplyLaiDaJinFuData.setIsLoans(0);

        String json = JsonUtils.toJson(orgApplyLaiDaJinFuData);
        //  加密
        AES cbc = AES.createCbc(privateKey.getBytes(), iv.getBytes());
        orgApplyLaiDaJinFu.setDataStr(cbc.encryptToBase64Str(json));

        String sendJson = JsonUtils.toJson(orgApplyLaiDaJinFu);
        return sendJson;

    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.str(jsonObject.getString("code"), "1")) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
